/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plist;

import aaa.Configurator;
import excel.CSVFile;
import excel.ExcelFile;
import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.plist.NSDictionary;
import net.sf.plist.NSObject;
import net.sf.plist.io.PropertyListException;
import net.sf.plist.io.PropertyListParser;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**
 *
 * @author Bashev
 */
public class PlistParser {
    
    private static IndexWriter writer;
    private static Directory index = new RAMDirectory();
    
    public static void addString(String resource) throws IOException
    {
       // System.out.println("Adding " + resource);
        Document contactDocument  = new Document();
        Field ff = new Field("content", resource, Field.Store.YES, Field.Index.ANALYZED);
        contactDocument.add(ff);       
        writer.addDocument(contactDocument);
    }
    
    public void parseP(String file) throws CorruptIndexException, LockObtainFailedException, IOException {
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);                
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
        writer = new IndexWriter(index, config);
        File f = new File(file);       
        NSDictionary dict = null;  
        try {
            dict = (NSDictionary)PropertyListParser.parse(f);
        } catch (PropertyListException ex) {
            readFile(f);
            return;
        }
        Map<String, NSObject> m = dict.map();         
        Iterator it = m.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            //System.out.println(pairs.getKey() + " = " + pairs.getValue());
            addString(pairs.getValue().toString());
        }
        writer.close();        
        
    }
    
    public void readFile(File f) throws FileNotFoundException{
        Pattern p = Pattern.compile("\" = \"([^\"]*)\""); 
        try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream(f);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-16LE"));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
            // Print the content on the console
                Matcher m = p.matcher(strLine);
                while (m.find()) {            
                    addString(m.group(1));
                }            
            }
            //Close the input stream
            in.close();
            writer.close();
            }catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }        
    }
    
    public static IndexReader write(String outputName) throws CorruptIndexException, CorruptIndexException, IOException{
        IndexReader ir = IndexReader.open(index);   
        index = new RAMDirectory();
        System.out.println(ir.document(1).get("content"));
        
        CSVFile csv = new CSVFile(ir); 
        csv.createCSVFile(outputName); 
        
        ExcelFile ef = new ExcelFile(ir);
        ef.createXLSFile(outputName);
        return ir;
    }
    
}
